package com.hongyuan.sellcoach.ui.member.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.CustomViewModel;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.MyRepository;
import com.hongyuan.sellcoach.ui.member.beans.MemberCourseBeans;
import com.hongyuan.sellcoach.ui.member.beans.MembersBean;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MemberCourseViewModel extends CustomViewModel<MyRepository> {

    public MemberCourseViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<MemberCourseItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<MemberCourseItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_member_course);


    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.CP_MEMBER){
            List<MemberCourseBeans.ListBean> list = ((MemberCourseBeans)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();

            }

            if(list != null && list.size() > 0){
                for(MemberCourseBeans.ListBean bean : list){
                    MemberCourseItemViewModel itemViewModel = new MemberCourseItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }
        }

    }
}
